//
//  ToastManager.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/8.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import Toast_Swift


struct ToastHelper {
    
    var hintQueue = NSMutableArray()
    static let shared = ToastHelper()
    
    func showTextToast(_ text: String, parent: UIView? = nil, duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, clearRepeat noRepeat: Bool = true, completion: ((_ didTap: Bool) -> Void)? = nil) {
        if noRepeat {
            if hintQueue.contains(text) {
                return
            }
            hintQueue.add(text)
        }
        if let view = parent {
            view.makeToast(text, duration: duration, position: position) {
                if noRepeat && hintQueue.contains(text) {
                    hintQueue.remove(text)
                }
                if completion != nil {
                    completion!($0)
                }
            }
        }else {
            showWindowToast(text, duration: duration, position: position) {
                if noRepeat && hintQueue.contains(text) {
                    hintQueue.remove(text)
                }
                if completion != nil {
                    completion!($0)
                }
            }
        }
    }
    
    func showViewToast(_ toast: UIView, parent: UIView? = nil, duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, completion: ((_ didTap: Bool) -> Void)? = nil) {
        if let view = parent {
            
        }
            
    }
    
}

func showWindowToast(_ text: String, duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, title: String? = nil, image: UIImage? = nil, style: ToastStyle = ToastManager.shared.style, completion: ((_ didTap: Bool) -> Void)? = nil) {
    
    UIWindow.keyWindow {
        $0.makeToast(text, duration: duration, position: position, title: title, image: image, style: style, completion: completion)
    }
    
}
